﻿using System;
using System.Collections.Generic;
using AMM.Business.Project;
using AMM.Model.Project;

namespace AMM.Forms.Reports.ReportUserControls {
    public partial class BOMCostReportSearchControl : BaseReportSearchControl {

        public override string ReportName {
            get { return "BOMCostReport.rdlc"; }
        }

        public override object CrossedThreadOperation() {
            string orderBy = this.cmbPrice.SelectedIndex == 0 ? "asc" : "desc";
            string bomValue = Convert.ToString(this.ddlBOMCode.SelectedValueEx);

            return this._reportProcessor.GetBOMCostViewList(bomValue, orderBy);
        }

        public override void ResetSearch() {
            this.cmbPrice.SelectedIndex = 0;
            this.ddlBOMCode.SelectedValueEx = null;
        }

        public BOMCostReportSearchControl() {
            this.InitializeComponent();
        }

        private void BindBOM() {
            BOMSearchModel searchModel = new BOMSearchModel() { IsEnabled = true, BOMStatus = 2 };
            BOMProcessor bomProcessor = new BOMProcessor(int.MaxValue);
            List<BOMViewModel> bomList = bomProcessor.FetchBOMViewModelList(searchModel);

            this.ddlBOMCode.DisplayMemberEx = "Code";
            this.ddlBOMCode.ValueMemberEx = "Code";
            this.ddlBOMCode.HeightEx = 300;
            this.ddlBOMCode.WidthEx = 400;
            this.ddlBOMCode.PageSizeEx = 20;
            this.ddlBOMCode.DataBind(new BOMViewModel(), x => new { x.Code, x.Description }, bomList);

            if (bomList.Count > 0 && this.ddlBOMCode.SelectedValueEx == null) {
                this.ddlBOMCode.SelectedValueEx = bomList[0].Code;
            }
        }

        private void BOMCostReportSearchControl_Load(object sender, EventArgs e) {
            this.BindBOM();
            this.cmbPrice.SelectedIndex = 0;
        }
    }
}
